package com.md.youyouhui.ui.user;

import android.databinding.DataBindingUtil;
import android.view.View;

import com.md.youyouhui.R;
import com.md.youyouhui.databinding.ActivityOrderDetailBinding;
import com.md.youyouhui.net.response.OrderDetailResponse;
import com.md.youyouhui.ui.BaseActivity;
import com.md.youyouhui.utils.MealMinuteCheckDialog;
import com.md.youyouhui.utils.ToastUtils;
import com.md.youyouhui.view.MainViewModel;

import java.util.List;

/**
 * Created by AdministratorZJ
 * on 2019/6/3.
 */
public class OrderDetailActivity extends BaseActivity {
    private ActivityOrderDetailBinding detailBinding;
    private MainViewModel viewModel;
    private List<OrderDetailResponse.Plan> plan;

    @Override
    public void setContentLayout() {
        detailBinding = DataBindingUtil.setContentView(this, R.layout.activity_order_detail);
        viewModel = new MainViewModel();
    }

    @Override
    public void initView() {
        setSupportActionBar(detailBinding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void initData() {
        detailBinding.orderDetailDialog.setOnClickListener(v -> new MealMinuteCheckDialog(ct, plan,detailBinding.orderDetailTitle.getText().toString()).show());
        int type = getIntent().getIntExtra("type", 10);
        if (type == 0) {
            detailBinding.orderDetailStatus.setText("进行中");
        } else {
            detailBinding.orderDetailStatus.setText("已完成");
        }
        String id = getIntent().getStringExtra("id");
        int orderType = getIntent().getIntExtra("orderType", 10);
        //0套餐
        if (orderType == 0) {
            detailBinding.orderDetailDialog.setVisibility(View.VISIBLE);
        } else {
            detailBinding.orderDetailDialog.setVisibility(View.GONE);
        }
        getOrders(id, orderType);
    }

    private void getOrders(String id, int type) {
        viewModel.getOrderInfoDetail(id, String.valueOf(type)).observe(this, listResource -> {
            assert listResource != null;
            switch (listResource.status) {
                case LOADING:
                    detailBinding.progress.setVisibility(View.VISIBLE);
                    break;
                case SUCCESS:
                    detailBinding.progress.setVisibility(View.GONE);
                    OrderDetailResponse data = listResource.data;
                    if (null == data) {
                        return;
                    }
                    plan = data.plan;
                    setDate(listResource.data);
                    break;
                case ERROR:
                    detailBinding.progress.setVisibility(View.GONE);
                    ToastUtils.errMake(this, listResource.errorCode);
                    break;
                default:
                    break;
            }
        });
    }

    private void setDate(OrderDetailResponse data) {
        detailBinding.orderDetailType.setText(data.cardType);
        detailBinding.orderDetailOilNum.setText(data.cardNo);
        detailBinding.orderDetailMonth.setText(data.monthTopUp);
        detailBinding.orderDetailContent.setText(data.orderContent);
        detailBinding.orderDetailCoupon.setText(data.useCoupons);
        detailBinding.orderDetailPrice.setText(data.payAmount);
        detailBinding.orderDetailNum.setText(data.orderNo);
        detailBinding.orderDetailWX.setText(data.payMethod);
        detailBinding.orderDetailTime.setText(data.payTime);
        detailBinding.orderDetailTitle.setText(data.orderContent);
        detailBinding.orderDetailPlanNum.setText(data.planNum);
    }
}
